# compile stage
FROM ccr.ccs.tencentyun.com/rootegg/node:pnpm-9.3.0 as build-stage

WORKDIR /appinstall

COPY package*.json pnpm-lock.yaml ./

RUN pnpm install --registry https://registry.npmmirror.com

COPY . .

RUN pnpm run build

# production stage
FROM ccr.ccs.tencentyun.com/rootegg/node:21.7.3-pm2-nginx-alpine as production-stage

ARG API_SERVER
ARG HOST_IP

WORKDIR /app

COPY --from=build-stage /appinstall/.output/ .

RUN echo -e "module.exports = { \n\
  apps: [{ \n\
    name: 'app', \n\
    exec_mode: 'cluster', \n\
    instances: 'max', \n\
    script: './server/index.mjs' \n\
  }] \n\
}" > ./ecosystem.config.js

RUN echo -e "\n\
server {  \n\
    listen       80; \n\
    proxy_http_version  1.1; \n\
    # nuxt服务端渲染
    location / { \n\
        proxy_pass  http://127.0.0.1:3000/; \n\
    } \n\
    # java后端接口
    location   /api/ { \n\
        proxy_pass              ${API_SERVER}/api/; \n\
        proxy_set_header        X-Real-IP \$remote_addr; \n\
        proxy_set_header        X-Forwarded-For \$proxy_add_x_forwarded_for; \n\
    } \n\   
    # 微前端文件 用qkpage做路由前缀时
    location ~* ^/cy-jeecg-ui/ { \n\
       proxy_pass              http://${HOST_IP}:5173; \n\
    } \n\
    keepalive_timeout  600; \n\
    fastcgi_connect_timeout 600; \n\
    fastcgi_send_timeout 600; \n\
    fastcgi_read_timeout 600; \n\
    proxy_connect_timeout 600; \n\
    proxy_send_timeout 600; \n\
    proxy_read_timeout 600; \n\
    send_timeout 600; \n\
    gzip on; \n\
    gzip_min_length 1k; \n\
    gzip_http_version 1.1; \n\
    gzip_comp_level 6; \n\
    gzip_types text/plain application/x-javascript text/css application/xml application/javascript; \n\
    gzip_vary on; \n\
    access_log  /var/log/nginx/access.log ; \n\
    client_max_body_size 100M; \n\
    client_body_buffer_size 100M; \n\
} " > /etc/nginx/http.d/default.conf
